<?php
/**
 * @file Specialist_postm.php
 * @synopsis  专家会诊贴
 * @author change
 * @version 1.0
 * @date 2015年10月16日15:17:59
 */


class Specialist_postm extends CI_Model
{
    function __construct()
    {
        parent::__construct();
        $this->load->Model("postm");
    }


	//添加会诊帖子帖子
    public function insert_specialist_post($data)
    {
        $result = $this->db->insert("ml_post_specialist",$data);
        if($result)
        {
            $return = $this->db->insert_id();
        }
        else
        {
            $return = false;
        }
        return $return;
    }

    /* 新增帖子字段 */
    public function insert_specialist_post_extend($data)
    {
        $result = $this->db->insert("ml_post_specialist",$data);
        if($result)
        {
            $return = $this->db->insert_id();
        }
        else
        {
            $return = false;
        }
        return $return;
    }

    //取用户相关的专家帖

    public function specialist_post_id_list_by_time($user_id,$begin_time,$end_time)
    {
        $all_post_id_list = $this->db->select("id")->from("ml_post")->where(array("author_id"=>$user_id,"is_show"=>1, "post_type"=>1,"addtime >"=>$begin_time, "addtime <"=>$end_time))->order_by("addtime","desc")->get()->result_array();
        $post_normal_id_list = array();
        foreach ($all_post_id_list as $value) {
            $post_normal_id_list[] = $value['id'];
        }
        return $post_normal_id_list;
    }
    //根据时间段获取发帖人专家帖

    public function invited_specialist_post_id_list_by_time($user_id,$begin_time,$end_time)
    {
        $this->db->select("id")->from("ml_post");
        $this->db->join('ml_invite', 'ml_invite.post_id = ml_post.id');
        $this->db->where(array("ml_invite.invite_id"=>$user_id, "ml_invite.type"=>1,"ml_post.is_show"=>1, "ml_post.post_type"=>1,"ml_post.addtime >"=>$begin_time, "ml_post.addtime <"=>$end_time));
        $all_post_id_list = $this->db->order_by("ml_post.addtime","desc")->get()->result_array();
        return $all_post_id_list;
    }
    //根据时间段获取被邀请人专家帖

    public function specialist_post_list($user_id, $page,$page_size)
    {
        $key = MY_SPECIALIST_LIST_PREFIX . $user_id . ':' . $page . ':' . $page_size;
        $post_list = unserialize($this->redis->get($key));
        if (empty($post_list)) {
            $post_ids = $this->specialist_post_id_list($user_id, $page, $page_size);
            foreach ($post_ids as $value) {
                //$one_post_info = $this->postm->get_one_post_4_search($value, $this->uid);
                $one_post_info = $this->postm->get_one_post_info($value, 1);
                if ($one_post_info) {
                    $post_list[] = $one_post_info;
                }
            }
            if ($post_list) {
                $this->redis->save($key, serialize($post_list), SPE_POST_INFO_STRING_KEY_EXPIRE_TIME);
            }
        }
        return $post_list;
    }

    // 临时加
    public function specialist_post_list_spe_user($user_id, $page, $page_size)
    {
        $post_ids = $this->specialist_post_id_list($user_id, $page, $page_size);
        return $post_ids;
    }

    public function specialist_post_id_list($user_id, $page,$page_size)
    {
        $start = ($page-1)*$page_size;
        $all_post_id_list = $this->db->select("id")->from("ml_post")->where(array("author_id" => $user_id, "is_show" => 1, "post_type" => 1))->order_by("id", "desc")->limit($page_size, $start)->get()->result_array();
        $post_normal_id_list = array();
        foreach ($all_post_id_list as $value) {
            $post_normal_id_list[] = $value['id'];
        }
        return $post_normal_id_list;
    }

    public function brand_post_list($user_id, $page, $page_size, $post_type_extend)
    {
        $post_list = array();
        $post_ids = $this->brand_post_id_list($user_id, $page, $page_size, $post_type_extend);
        if(!$post_ids)
        {
            return false;
        }
        foreach($post_ids as $value)
        {
            $get_one_post_content = $this->postm->get_one_post_4_search($value, $this->uid);
            $post_list[] = $get_one_post_content;
        }
        return $post_list;
    }


    /* 根据分类或者标签来获取帖子列表 */

    public function brand_post_id_list($user_id, $page, $page_size)
    {
        $start = ($page-1)*$page_size;
        $filters = array('author_id' => $user_id, 'is_show' => 1, 'is_pinpai' => 2);
        $all_post_id_list = $this->db
            ->select('id')
            ->from('ml_post')
            ->where($filters)
            ->order_by('id')
            ->limit($page_size, $start)
            ->get()
            ->result_array();
        if(!$all_post_id_list)
        {
            return false;
        }
        $post_normal_id_list = array();
        foreach ($all_post_id_list as $value)
        {
            $post_normal_id_list[] = $value['id'];
        }
        return $post_normal_id_list;
    }

    /* 获取投票选项 */

    public function vote_post_id_list_thread($user_id,$page,$page_size,$post_type_extend,$dis_cate_flag='',$thread_id='',$tag_id='')
    {
        $start = ($page-1)*$page_size;
        if($dis_cate_flag == 1){
            $where = array("a.is_show"=>1, "a.post_type_extend"=>$post_type_extend,"b.type_id"=>$thread_id,'b.type'=>1);
            if($post_type_extend == 100){
                $where = array("a.is_show"=>1, "a.is_pinpai"=>2,"b.type_id"=>$thread_id,'b.type'=>1);
            }
        }else{
            $where = array("a.is_show"=>1, "a.post_type_extend"=>$post_type_extend,"b.type_id"=>$thread_id,'b.type'=>2);
            if($post_type_extend == 100){
                $where = array("a.is_show"=>1, "a.is_pinpai"=>2,"b.type_id"=>$thread_id,'b.type'=>2);
            }
        }
        $all_post_id_list = $this->db
        ->select("a.id")
        ->from("ml_post as a")
        ->where($where)
        ->join('ml_posttype as b','a.id=b.post_id','left')
        ->order_by("a.addtime","desc")
        ->limit($page_size,$start)
        ->get()
        ->result_array();
        $post_normal_id_list = array();
        if($all_post_id_list)
        {
            foreach ($all_post_id_list as $value) {
                $post_normal_id_list[] = $value['id'];
            }
        }
        return $post_normal_id_list;
    }

    /* 查询总投票信息 */

    public function get_click_vote($post_id,$author_id)
    {
        $res = $this->db->select('option_id')->from('ml_vote_relation')->where('clicker_id',$author_id)->where('post_id',$post_id)->get()->result_array();
        $sql = $this->db->last_query();
        return $res[0]['option_id'];
    }

    /* 查询投票信息 */

    public function get_toal_count($post_id)
    {
        $res = $this->db->select('sum(count) as count')->from('ml_vote_option')->where('post_id',$post_id)->get()->result_array();
        return $res[0]['count'];
    }

    //取用户相关的投票帖

    public function get_vote_list($post_id)
    {
        $res = $this->db->select('id,option,count,sort')->from('ml_vote_option')->where('post_id',$post_id)->order_by('sort')->get()->result_array();
        return $res;
    }


    // 2.2.2 优化个人中心返回所有的专家咨询帖
    public function get_spe_count($author_id)
    {
        $filters = array('author_id' => $author_id, 'is_show' => 1, 'post_type' => 1);
        $post_count = $this->db
            ->where($filters)
            ->group_start()
            ->or_where(array('post_type' => 1, 'post_type_extend' => 6))
            ->group_end()
            ->count_all_results("ml_post");
        return $post_count;
    }

    public function vote_post_list($user_id, $page, $page_size, $post_type_extend)
    {
        $post_list = array();
        $post_ids = $this->vote_post_id_list($user_id, $page, $page_size, $post_type_extend);
        if (!$post_ids) {
            return false;
        }
        foreach ($post_ids as $value) {
            $get_one_post_content = $this->postm->get_one_post_4_search($value, $this->uid);
            $post_list[] = $get_one_post_content;
        }
        return $post_list;
    }

    /* 根据分类或者标签来获取帖子列表 */

    public function vote_post_id_list($user_id, $page, $page_size, $post_type_extend)
    {
        $start = ($page - 1) * $page_size;
        if ($post_type_extend == 4 || $post_type_extend == 5) {
            $sql = 'select id from ml_post where author_id=' . $user_id . ' and is_show=1 and post_type_extend in(4,5) order by addtime desc limit ' . $start . ',' . $page_size;
        } else {
            if ($post_type_extend != 100) {
                $sql = 'select id from ml_post where author_id=' . $user_id . ' and is_show=1 and post_type_extend =' . $post_type_extend . ' order by addtime desc limit ' . $start . ',' . $page_size;
            } else {
                $sql = 'select id from ml_post where author_id=' . $user_id . ' and is_show=1 and is_pinpai =2 order by addtime desc limit ' . $start . ',' . $page_size;
            }
        }
        $all_post_id_list = $this->db->query($sql)->result_array();
        if (!$all_post_id_list) {
            return false;
        }
        $post_normal_id_list = array();
        foreach ($all_post_id_list as $value) {
            $post_normal_id_list[] = $value['id'];
        }
        return $post_normal_id_list;
    }
}